local skynet = require "skynet"
local filelog = require "filelog"
local filename = "agentmgrcmd.lua"
local service = nil

local agentmgrcmd = {
	service = nil,
}

function agentmgrcmd.process(session, source, event, ...)
	if not service then
		service = agentmgrcmd.service
	end
	local f = agentmgrcmd[event] 
	if f == nil then
		filelog.sys_error(filename.." line17 agentmgrcmd.process invalid event:"..event)
		return nil
	end
	skynet.retpack(f(...))
end

function agentmgrcmd.start(conf)
	service.tcpmng.init(service, "agent", conf.agentsize, conf.agentincr, "websocketnetpack")

	skynet.fork(function()
		skynet.sleep(100)
		skynet.call(service.gate_service, "lua", "open", conf)
	end)
	return true
end

function agentmgrcmd.close(...)
	service:exit_service()	
end

-- 这里和 loginnodecmd.agentexit 有不同，要带上rid
-- LTM 需要rid 才能去找到对应的agent
function agentmgrcmd.agentexit(fd, rid)
	service.tcpmng.agentexit(fd, rid)
end

--处理玩家心跳超时关闭socket
function agentmgrcmd.heart_timeout(fd)
	service.tcpmng.heart_timeout(fd)
end

-- 得到某个agent的service，主要是为了使用里面的 long_tcp_mng
function agentmgrcmd.get_agent_by_rid( ...)
	return service:get_agent_by_rid( ... )
end

function agentmgrcmd.set_tableaddress_by_rid(rid, tableaddress)
	return service:set_tableaddress_by_rid(rid, tableaddress)
end

function agentmgrcmd.clear_tableaddress_by_rid(rid)
	return service:clear_tableaddress_by_rid(rid)
end

function agentmgrcmd.notify(rids, msgname, msgbody, retcode, retstr)
	return service:notify(rids, msgname, msgbody, retcode, retstr)
end

return agentmgrcmd